package cnpc.udp.example.controller;


import cnpc.udp.example.controller.vo.TestPageVO;
import cnpc.udp.example.controller.vo.TestVO;
import cnpc.udp.example.convert.TestConvert;
import cnpc.udp.example.dataobject.TestDO;
import cnpc.udp.example.service.TestService;
import cnpc.udp.framework.basic.common.datastructure.PageResult;
import cnpc.udp.framework.basic.common.datastructure.UdpResult;
import cnpc.udp.framework.basic.common.utils.CollectionUtil;
//import cnpc.udp.framework.swagger.annotation.UdpDocGroup;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static cnpc.udp.framework.basic.common.datastructure.UdpResult.success;

/**
 * @ClassName TestController
 * @Description TODO
 * @Author Tiansai
 * @Date 2024年05月24日 11:07
 **/
@Tag(name = "测试")
@RestController
@RequestMapping("/test")
@Validated
public class TestController {

    @Resource
    private TestService testService;


    /**
     * 新增一个Test对象
     *
     * @param reqVO 包含Test对象信息的VO对象
     * @return 返回一个UdpResult对象，其中包含新增操作的结果
     */
    @PostMapping("/create")
    @Operation(summary = "新增")
    public UdpResult<Boolean> create(@Valid @RequestBody TestVO reqVO) {
        TestDO testDO = TestConvert.INSTANCE.convertDo(reqVO);
        testService.save(testDO);
        return success(true);
    }

    @PostMapping("/insert")
    @Operation(summary = "新增")
    public UdpResult<Boolean> insert(@Valid @RequestBody TestVO reqVO) {
        testService.insert(reqVO);
        return success(true);
    }


    @PutMapping("update")
    @Operation(summary = "修改")
    public UdpResult<Boolean> update(@Valid @RequestBody TestVO reqVO) {
        testService.saveOrUpdate(TestConvert.INSTANCE.convertDo(reqVO));
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public UdpResult<Boolean> delete(@RequestParam("id") String id){
        testService.removeById(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获取")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public UdpResult<TestVO> get(@RequestParam("id") String id) {
        TestDO testDO = testService.getById(id);
        // 拼接数据
        return success(TestConvert.INSTANCE.convertVo(testDO));
    }

    @GetMapping("/getList")
    @Operation(summary = "获取列表")
    public UdpResult<List<TestVO>> getList() {
        return success(testService.getList());
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户分页列表")
    public UdpResult<PageResult<TestVO>> getUserPage(@Valid TestPageVO pageReqVO) {
        // 获得用户分页列表
        PageResult<TestDO> pageResult = testService.getPage(pageReqVO);
        if (CollectionUtil.isEmpty(pageResult.getList())) {
            return success(new PageResult<>());
        }
        return success(new PageResult<>(pageResult.getTotal(),
                TestConvert.INSTANCE.convertVos(pageResult.getList())));
    }
}
